'''
绘制曲线
'''
import numpy as np
import matplotlib.pyplot as mp

#从-Π到Π之间取1000个点

x=np.linspace(-np.pi,np.pi,1000)
#设置y坐标
sinx = np.sin(x)
cosx = np.cos(x)/2
#修改横坐标数值latex语法
#r是为了不让\和p成为一个字符
names =[r'$-\pi$',r'$-\frac{\pi}{2}$',0,r'$\frac{\pi}{2}$',r'$\pi$']#显示的样式
mp.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],names)#x轴对应的值

#设置坐标轴
#获取坐标轴字典
#['left':"左轴",'right':"右轴",'top':"上轴",'bottom':"下轴"]
ax = mp.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
#set_position():设置坐标轴的位置,传入的两个元素的元组为参数
ax.spines['left'].set_positon(('data',0))
ax.spines['bottom'].set_positon(('data',0))

#绘图  label = r'$y=cos(x)$':图例参数
#linestyle:线型，linewidth：线宽 color:颜色 alpha：颜色深度
mp.plot(x,cosx,linestyle="--",color = 'pink',label = r'$y=cos(x)$')
mp.plot(x,sinx,linewidth = 7,alpha = 0.5,label = r'$y=sin(x)$')

#设置特殊点
xs,ys = [-np.pi/2,np.pi/2],[0,1]
mp.scatter(xs,ys,marker='D',edgecolors='red',facecolor='green',s=100,label='Points',zorder = 3)

#设置备注文本
mp.annotate(r'$[\frac{\pi}{2},1]$',xycoords = 'data',xy=(np.pi/2,1),textcoords = 'offset points',xytext = (30,20),fontsize=14,arrowprops = dict(arrowstyle = '->',connectionstyle = 'angle3'))
mp.annotate(r'$[-\frac{\pi}{2},0]$',xycoords = 'data',xy=(-np.pi/2,0),textcoords = 'offset points',xytext = (30,20),fontsize=14,arrowprops = dict(arrowstyle = '->',connectionstyle = 'angle3'))

#设置图例
mp.legend()

mp.show()